package com.Drools.转换.校验;

import com.alibaba.fastjson.JSONObject;
import org.mvel2.util.Make;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.util.*;

public class JS_Ruler {

    public static void main(String[] args) {
        Object re = TR003();

        Map map = JSONObject.parseObject(re.toString());

    }
    public static Object _23E(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\集成.js",
                "_23E",
                "SDVA"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object _26T(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\集成.js",
//                "_26T",
                "_26T",
                "K90"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object TR015(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\TR015.js",
//                "_57D",
                "MT_To_MXClearingIdentifier",
                "//RT//ATBLZ1234567889\na5sd5s"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object _57D(){

        List l = new ArrayList();
        l.add("//RT4566845612465\nw6e5fe8da6s45fc5eds");
        l.add("//RT4566845612465\nw6e5fe8da6s45fc5eds");
        l.add("//RT4566845612465\nw6e5fe8da6s45fc5eds");
        Map<String,Object> m = new HashMap<>();
        m.put("a",l);
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\集成.js",
//                "_57D",
                "MT_To_MXClearingIdentifier",
//                "//RT4566845612465\nw6e5fe8da6s45fc5eds\nswdwad"
//                "ajsidaidoajijoisdjaadaodkoakdopadae3/wdadauhdiaihdiuahwu"
                "//RT//ATBLZ1234567889\na5sd5s"
//                "//RT4566845612465\nw6e5fe8da6s45fc5eds"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object _59(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\李章龙.js",
                "_59",
                "/INTA/11111111111111111111111111\n" +
                        "/REC/111111111111111111111111111111\n" +
                        "/REC/222222222222222222222222222222\n" +
                        "/REC/333333333333333333333333333333\n" +
                        "/REC/444444444444444444444444444444\n" +
                        "/REC/555555555555555555555555555555\n" +
                        "/REC/666666666666666666666666666667",
                ""
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object METAFCT001(){
        Object mt_to_mxPartyAccount;
        Map<String, Object> MT = getMTtr001();
//        mt_to_mxPartyAccount = getMETACT(MT);//100

        Map<String,Object> MT53A = new LinkedHashMap<>();
        MT.put("MT53A",MT53A);
        MT53A.put("555","555");
        Map<String,Object> MT53B = new LinkedHashMap<>();
        MT.put("MT53B",MT53B);
        MT53B.put("PartyIdentifier","/C1222");//if
        //MT53B.put("PartyIdentifier","/D1222");//else
        MT53B.put("Location","111");

//        mt_to_mxPartyAccount = getMETACT(MT);//210

        MT53A.put("PartyIdentifier","/C1222");//if
//        MT53A.put("PartyIdentifier","/D1222");//else
        MT53A.put("BIC","CND");

//        mt_to_mxPartyAccount = getMETACT(MT);//221

        MT53A.remove("PartyIdentifier");
//        MT53A.put("BIC","ABCDEFGHIGKLMN");//if
        MT53A.put("BIC","8966545522");//else
//        mt_to_mxPartyAccount = getMETACT(MT);//222

        Map<String,Object> MT53D = new LinkedHashMap<>();
        MT.put("MT53D",MT53D);
        MT53A.put("PartyIdentifier","/C1222");//if
//        MT53A.put("PartyIdentifier","/D1222");//else
        MT53D.put("PartyIdentifier","/Casdsdsads");
//        mt_to_mxPartyAccount = getMETACT(MT);//230

        MT53D.remove("PartyIdentifier");
//        mt_to_mxPartyAccount = getMETACT(MT);//231

        MT.remove("MT53A");
        Map<String,Object> MT54A = new LinkedHashMap<>();
        MT.put("MT54A",MT54A);
        MT54A.put("BIC","123456789");
//        mt_to_mxPartyAccount = getMETACT(MT);//311
        MT54A.put("BIC","95523456789");
        Map<String,Object> MT54B = new LinkedHashMap<>();
        MT.put("MT54B",MT54B);
        Map<String,Object> MT54D = new LinkedHashMap<>();
        MT.put("MT54D",MT54D);
//        mt_to_mxPartyAccount = getMETACT(MT);//312

        MT.put("MT53A",MT53A);
//        MT53A.put("BIC","12345678");//if
        MT53A.put("BIC","123456789");//else

        MT.put("MT54A",MT54A);
        //MT54A.put("BIC","12345678");//if
        MT54A.put("BIC","ABCDEFGHIGKLMN");//else
        Map<String,Object> MT55A = new LinkedHashMap<>();
        MT.put("MT55A",MT55A);
//        mt_to_mxPartyAccount = getMETACT(MT);//411/412/413

        MT53A.put("BIC","12345678");
//        MT53A.put("PartyIdentifier","/C55555");//if
        MT53A.put("PartyIdentifier","/D55555");//else
//        mt_to_mxPartyAccount = getMETACT(MT);//413 else if


        MT53A.put("BIC","123");
//        mt_to_mxPartyAccount = getMETACT(MT);//413 else

        MT.put("MT53B",MT53B);
        MT.put("MT54A",MT54A);
//        MT53B.put("PartyIdentifier","/C11111");//if   if
        MT53B.put("PartyIdentifier","/D2222222");//if   else
        MT53B.put("PartyIdentifier","//asdsds");//else
        MT53B.put("Location","Location");
        MT.put("MT55A",MT55A);
//        mt_to_mxPartyAccount = getMETACT(MT);//421

        MT.put("MT53D",MT53D);
        MT.put("MT54A",MT54A);
        MT.put("MT55A",MT55A);
        MT53D.put("PartyIdentifier","/C11111");//if if
        MT53D.put("PartyIdentifier","/D11111");//if else
        MT53D.put("PartyIdentifier","//11111");//else

//        mt_to_mxPartyAccount = getMETACT(MT);//431

        MT.put("MT55A",MT55A);
        MT.put("MT53A",MT53A);
        MT.put("MT54B",MT54B);MT.put("MT54D",MT54D);//选其一 二个都有也可
        MT53A.put("BIC","123456");
        MT53A.put("PartyIdentifier","/C8741");//if
        MT53A.put("PartyIdentifier","/D88546");//else
//        mt_to_mxPartyAccount = getMETACT(MT);//441 if

        MT53A.put("BIC","123");
        mt_to_mxPartyAccount = getMETACT(MT);//441 else

        return mt_to_mxPartyAccount;
    }

    private static Object getMETACT(Map<String, Object> MT) {
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3简写.js",
                "METAFCT001",
                MT
        );

        System.out.println("------------");
        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }

    public static Object IsMTClearingSystemCodeInList(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "IsMTClearingSystemCodeInList",
//                "//RT//ATBLZ12343546576898",
//                "//RT//AU12343546576898",
                "//RT//AA12343546576898"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object TR011(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "TR011",
                "/INTA/11111111111111111111111111\n" +
                        "/REC/111111111111111111111111111111\n" +
                        "/REC/222222222222222222222222222222\n" +
                        "/REC/333333333333333333333333333333\n" +
                        "/REC/444444444444444444444444444444\n" +
                        "/REC/555555555555555555555555555555\n" +
                        "/REC/666666666666666666666666666667",
                ""
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object TR004(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "TR004",
                "USD35,\nUSD35,",
                "USD",
                "35,"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }

    public static Object Trim(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "TrimRight",
//                "TrimLeft",
//                "000111",
                "000111",
                "111"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object TR001(){

        Map<String, Object> MT = getMTtr001();
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "TR001",
                MT
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }

    private static Map<String, Object> getMTtr001() {
        Map<String,Object> MT = new LinkedHashMap<>();
        Map<String,Object> BAH = new LinkedHashMap<>();
        Map<String,Object> From = new LinkedHashMap<>();
        Map<String,Object> ff = new LinkedHashMap<>();
        Map<String,Object> fff = new LinkedHashMap<>();
        Map<String,Object> To = new LinkedHashMap<>();
        Map<String,Object> tf = new LinkedHashMap<>();
        Map<String,Object> tff = new LinkedHashMap<>();
        MT.put("BAH",BAH);
        BAH.put("From",From);
        BAH.put("To",To);
        From.put("FinInstnId",ff);
        To.put("FinInstnId",tf);
        tf.put("FinInstnId",tff);
        ff.put("FinInstnId",fff);
        tff.put("BICFI","Sender");
        fff.put("BICFI","Receiver");

        tff.put("BICFI","ABCDEFGHIGKLMN");
        fff.put("BICFI","123456789");

        return MT;
    }

    public static Object TR003(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "TR003",
//                "1/qwwads\n2/adawsd\n3/AB/LEIC/0123456789ABCDEFGH12\n3/AB/LEIC/asds",
                "1/qwwads\n2/adawsd\n3/AB/LEIC/0123456789ABCDEFGH12\n3/qq/LEIC/asds"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object MT_To_MXPartyNameAndStructuredAddress(){

        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "MT_To_MXPartyNameAndStructuredAddress",
//                "1/qwwads\n2/adawsd\n3/AB/LEIC/0123456789ABCDEFGH12\n3/AB/LEIC/asds",
                "1/qwwads\n2/adawsd\n3/AB/LEIC/0123456789ABCDEFGH12\n3/qq/LEIC/asds"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object MT_To_MXIntermediaryAgent(){
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "MT_To_MXIntermediaryAgent",
//                "qwertw12",
//                "ATBLZqweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
//                "ATBLZqweeeeee",
//                "1111123123232CN211(323",
                "1111123123232(CN211(323"
        );

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object IsValidBIC(){
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "IsValidBIC",
                "ATBLZsadfasd");

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object Substring(){
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "Substring",
                "123456",2,2);

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object MT_To_MXField72NewCodeWords(){
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "MT_To_MXField72NewCodeWords",
                "/SVCLVL/G00812222222\n/CATPURP/qw111111111");

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }
    public static Object DeletePattern(){
        Object mt_to_mxPartyAccount = excuteJs(
                "D:\\Win10Driver\\OneDrive\\桌面\\案例\\3.1.js",
                "DeletePattern",
                "23123/INTA/INTA/ds8dasd\n//666\n/awd/awdas\n//ads\n/adsdadawfawfa/5ad4a5",
                "/INTA/ds8dasd //666/awd/awdas //ads");

        System.out.println(mt_to_mxPartyAccount);
        return mt_to_mxPartyAccount;
    }

    public static Object excuteJs(String classPath, String methodName, Object ...msg){

        boolean res = false;

        //取得脚本引擎的管理器Manager
        ScriptEngineManager scriptManager= new ScriptEngineManager();

        //根据名称取得js的脚本引擎
        ScriptEngine engine = scriptManager.getEngineByName("javascript");

        try {
            //读js文件
            FileReader fileReader = new FileReader(classPath);

            //执行文件
            engine.eval(fileReader);

            if(engine instanceof Invocable){

                Invocable invoke = (Invocable)engine;

                //执行校验方法
                Object result= invoke.invokeFunction(methodName, msg);

                return result;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;

    }


    public static Map<String,Object> putAll(Map<String,Object> a,Map<String,Object> b){
        Map<String,Object> c = a;
        //有泛型  才可以
        for (Map.Entry entry :b.entrySet()){
            Object o = c.get(entry.getKey());
            if(o == null){
                c.put(entry.getKey().toString(),entry.getValue());
            }else{
                if(entry.getValue() instanceof Map &&
                        o instanceof Map){
                    putAll((Map<String, Object>) o, (Map<String, Object>) entry.getValue());
                }else if(entry.getValue() instanceof List && o instanceof List){
                    List<Object> l1 = (List<Object>) o;
                    List<Object> l2 = (List<Object>) entry.getValue();
                    l1.addAll(l2);
                }
            }
        }

        return c;

    }

    public static void setMap(Map<String,Object> map,String str,Object o){
        String[] ss = str.split("\\.");
        Map<String,Object> m = map;
        for (int i = 0; i < ss.length - 1; i++) {
            if(m.get(ss[i]) == null){
                Map<String,Object> mm = new HashMap<>();
                m.put(ss[i],mm);
            }
            m = (Map<String, Object>) m.get(ss[i]);
        }
        m.put(ss[ss.length-1],o);

    }

}
